from PyQt4 import QtCore, QtGui;

class GrpLabel_Item(QtGui.QGraphicsItem):
    def __init__(self, aGrpLbl_Texts, aTrackPaths_Grp, gSceneView):
        super(GrpLabel_Item, self).__init__();
        self.aGrpLbl_Texts = aGrpLbl_Texts;
        self.aTrackPaths_Grp = aTrackPaths_Grp;
        self.gSceneView = gSceneView; #SceneView of Graph Nodes
        self.setToolTip("\n".join(aGrpLbl_Texts));
        self.getGrpIds();
        self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True);
        self.setCursor(QtCore.Qt.PointingHandCursor); #Show PointingHand Cursor Style
        #This is the whole "Drag & Drop" feature needed to move GraphicsItems!!!
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True);
        self.createStateShape();
        self.createTexts();
        self.setStyleStatus_Normal();
        self.setVisible(False);
    
    def getGrpIds(self):
        self.aGrpIds = [];
        for grpLbl in self.aGrpLbl_Texts:
            self.aGrpIds.append(int(grpLbl.split('.')[0]));
    
    def boundingRect(self):
        return self.qStateItem.boundingRect();
        
    def paint(self, painter, option, widget):
        return;
        
    def itemChange(self, change, value):
        if change == QtGui.QGraphicsItem.ItemSelectedHasChanged:
            self.setSelectedStyle(self.isSelected());
            self.gSceneView.setNodes_Selected(self.aGrpIds, self.isSelected());
            for trk in self.aTrackPaths_Grp:
                trk.setSelectedStyle(bSelected=self.isSelected());
        return QtGui.QGraphicsItem.itemChange(self, change, value);
    
    def setSelectedStyle(self, bSelected):
        if bSelected: self.setStyleStatus_Selected();
        else: self.setStyleStatus_Normal();
    
    def setStyleStatus_Normal(self):
        self.qStateItem.setPen(QtGui.QPen(QtGui.QColor(0,0,0, 100), 1));
        self.qTxtGrpLbl.setDefaultTextColor(QtGui.QColor(0,0,0, 255));
        self.qTxtGrpLbl.setFont(QtGui.QFont("Arial", 7));
        
    def setStyleStatus_Selected(self):
        self.qStateItem.setPen(QtGui.QPen(QtGui.QColor(0,0,255, 255), 2));
        self.qTxtGrpLbl.setDefaultTextColor(QtGui.QColor(0,0,255, 255));
        self.qTxtGrpLbl.setFont(QtGui.QFont("Arial", 7, QtGui.QFont.Bold));
        
        
    def createTexts(self):
        self.qTxtGrpLbl = QtGui.QGraphicsTextItem(self);
        self.qTxtGrpLbl.setTextWidth(200);
        aGrpLbl_Texts = map(lambda s: s.replace('<','&lt;').replace('>','&gt;'), 
                            self.aGrpLbl_Texts);
        tAlign = "<div align='left'>" + "<br>".join(aGrpLbl_Texts);
        self.qTxtGrpLbl.setHtml(tAlign + "</div>");
        self.qTxtGrpLbl.setDefaultTextColor(QtGui.QColor(0,0,0, 255));
        self.qTxtGrpLbl.setFont(QtGui.QFont("Arial", 7));
        nH = self.qTxtGrpLbl.boundingRect().height();
        self.posGrpLbl = QtCore.QPointF(self.qRect.x(), -nH+3);
        self.qTxtGrpLbl.setPos(self.posGrpLbl);
   
    def createStateShape(self):
        nX, nY, nWidth, nHeight = 0,0, 100,100;
        self.qRect = QtCore.QRectF(nX,nY, nWidth,nHeight);
        qPath = QtGui.QPainterPath();
        qPath.addRoundedRect(self.qRect, 5,5);
        self.qStateItem = QtGui.QGraphicsPathItem(qPath, self);
        self.qStateItem.setBrush(QtGui.QBrush(QtGui.QColor(190,190,190, 60)));
        
    def updateSize(self, nFrm):
        aX, aY, nP = [], [], 10;
        for trk in self.aTrackPaths_Grp:
            aX.append(trk.getPos(nFrm)[0]);
            aY.append(trk.getPos(nFrm)[1]);
        nMinX, nMinY, nMaxX, nMaxY = min(aX), min(aY), max(aX), max(aY);
        self.setSize(nMinX-nP, nMinY-nP, nMaxX-nMinX+nP*2, nMaxY-nMinY+nP*2);
        
    def setSize(self, x, y, w, h):
        self.setPos(x,y);
        self.qRect = QtCore.QRectF(0,0, w,h);
        qPath = QtGui.QPainterPath();
        qPath.addRoundedRect(self.qRect, 5,5);
        self.qStateItem.setPath(qPath);
        
        